import scrapy


# from bili.bili.items import BiliItem

class BiliRankeSpider(scrapy.Spider):
    name = 'bili_ranke'
    # allowed_domains = ['www.xxx.com']
    # https://www.bilibili.com/v/popular/rank/all
    start_urls = ['https://www.bilibili.com/v/popular/rank/all']

    def parse(self, response):
        # 第一种
        item = {}
        _id = 0
        titles = response.xpath('//a[@class="title"]/text()').getall()
        play_nums = response.xpath('//div[@class="detail"]/span[@class="data-box"][1]/text()').getall()
        up_names = response.xpath('//div[@class="detail"]/a/span/text()').getall()
        scores = response.xpath('//div[@class="pts"]/div/text()').getall()
        for title, play_num, up_name, score in zip(titles, play_nums, up_names, scores):
            item["_id"] = _id
            _id += 1
            # 第二种方法
            # item = BiliItem()
            # print(up_name.strip())
            item["title"] = title
            item["play_num"] = play_num.strip()
            item["up_name"] = up_name.strip()
            item["score"] = score
            yield item  # 这是我的常用方法(我常用第一种方法)
